from aip import AipOcr
import os
from openpyxl import Workbook
""" 你的 APPID AK SK """
APP_ID = '*******'
API_KEY = '********'
SECRET_KEY = '********'

client = AipOcr(APP_ID, API_KEY, SECRET_KEY)
wb=Workbook()
sheet=wb.active
sheet.append(['姓名','民族','住址','身份证号码','出生日期','性别'])

""" 读取图片 """
def get_file_content(filePath):
    with open(filePath, 'rb') as fp:
        return fp.read()
files=os.listdir('C:/Users/Lenovo/Desktop/身份证/')
for aaa in files:
    image = get_file_content('C:/Users/Lenovo/Desktop/身份证/'+aaa)
    idCardSide = "back"
    # 调用身份证识别
    res_image = client.idcard(image, idCardSide)
    aa=(res_image['words_result']['姓名']['words'])
    bb=(res_image['words_result']['民族']['words'])
    cc=(res_image['words_result']['住址']['words'])
    dd=(res_image['words_result']['公民身份号码']['words'])
    ee=(res_image['words_result']['出生']['words'])
    ff=(res_image['words_result']['性别']['words'])
    sheet.append([aa,bb,cc,dd,ee,ff])

wb.save('身份证.xlsx')
